package com.pinyoumei.app.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;

import com.pinyoumei.app.R;
import com.pinyoumei.app.utils.DensityUtils;

/**
 * 自定义图层ImageView(在ImageView图片上写一个二维码图层)
 */
public class CenterImage extends android.support.v7.widget.AppCompatImageView{
    private Paint paint_image;
    private Paint paint_text;
    private boolean isCenterImgShow;
    private Bitmap bitmap;
    private String recommend_code;
    private Context context;
    public void setCenterImgShow(boolean centerImgShow,Bitmap bitmap,String recommend_code){
        isCenterImgShow = centerImgShow;
        if (isCenterImgShow){
            this.bitmap = bitmap;
            this.recommend_code = recommend_code;
            invalidate();
        }
    }
    public CenterImage(Context context){
        super(context);
        this.context = context;
        init();
    }
    public CenterImage(Context context, AttributeSet attrs){
        super(context, attrs);
        this.context = context;
        init();
    }
    public CenterImage(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }
    private void init(){
        //图片的画笔
        paint_image = new Paint();
        //文字的画笔
        paint_text = new Paint();
        paint_text.setTextSize(DensityUtils.sp2px(context,8));
        // 将画笔设置为描边
        paint_text.setStyle(Paint.Style.FILL);
       // 设置线条宽度
        paint_text.setStrokeWidth(2);
        paint_text.setColor(ContextCompat.getColor(context,R.color.recommend_text_color));
    }
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        if (isCenterImgShow && bitmap != null){
            canvas.drawBitmap(bitmap, getMeasuredWidth() - bitmap.getWidth()-20 , getMeasuredHeight() - bitmap.getHeight()-10, paint_image);
            canvas.drawText("推荐码:"+recommend_code,getMeasuredWidth() - getMeasuredWidth()*3/4+10,getMeasuredHeight()-40,paint_text);//getMeasuredWidth() - bitmap.getWidth()*2-70
        }
    }
}
